package cuit.game.controller;

import cuit.game.entity.Contest;
import cuit.game.entity.Team;
import cuit.game.service.ContestService;
import cuit.game.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/contest")    
public class ContestController {

    //注入类
    @Autowired
    private ContestService contestService;

    @Autowired
    private TeamService teamService;

    //项目的分页展示
    @GetMapping("/view")
    public String view(@RequestParam(value = "page", defaultValue = "0") int page,
                       @RequestParam(value = "size", defaultValue = "5") int size,
                       Model model) {

        Page<Contest> pageContest = contestService.findAllByPage(page, size);

        model.addAttribute("pageContest", pageContest);//将分页列表传到前端

        return "/contest/view";
    }

    //新建项目
    @GetMapping("/toadd")
    public String add(Model model) {
    	Contest sd=new Contest();
    	model.addAttribute("contest",sd);
    	
    	return "/contest/add";}

    @PostMapping("/save")
    public String save(@ModelAttribute("contest") Contest contest) {

        contestService.add(contest);
        return "redirect:/contest/view";
    }

    //搜索项目
    @GetMapping("/search")
    public String search() {return "search";}

    @GetMapping("/return")
    public String returnContest(@RequestParam("contestId") int contestId, Model model) {

        Contest contest = contestService.findById(contestId);
        model.addAttribute("contest", contest);
        return "/contest/search";
    }

    //删除项目
    @GetMapping("/delete")
    public String delete(@RequestParam("contestId") int contestId) {
        contestService.deleteById(contestId);
        return "redirect:/contest/view";
    }

}
